<?php
uses('controller');

class mailController extends Controller {

    var $subject;
    var $sendto;
    var $mailto;
    var $ishtml;
    var $body;
    var $sendtime;

    function __construct() {
        parent::__construct();
    }

    /**
     * 礼品卡发送
     *
     * @param mixed $data
     * @access public
     * @return int 1.成功
     */
    function sendGiftCard($data) {
        $this->reset();

        $meta = unserialize($data['meta']);
        $meta = $meta['gift'];

        $vc =& getController('voucher');
        $v['str']   = $data['vc'];
        $v['price'] = $data['sumfee'];
        $result = $vc->getVoucher($v);
        $expire = date('Y-m-d', $result['expiredtime']);

        //通知购买人
        $this->setSubject("你在乐铺购买的礼品卡");
        $this->setSendTo($data['cman']);
        $this->setMailTo($data['cemail']);
        $this->isHTML(true);

        //邮件模板
        $tpl = ROOT.'edm/giftcard/mail.html';

        $memo = nl2br($data['memo']);
        $date = date('Y/m/d');

        $body = <<< BODY
            <div style="font-size:14px;line-height:160%">
            <center><img vspace=10 src="http://www.loopo.cn/edm/giftcard/icon.png" width="187" height="106" alt="乐铺礼品卡" /></center>
            {$data['cman']}：<br />
            感谢您选择乐铺。您购买的礼品卡已经生效，相关信息如下：
            <hr size=1 />
            <blockquote>
                礼品卡代码：<font style="font-size:16px;font-family:Verdana"><b>{$result['str']}</b></font><br />
                礼品卡面值：<font style="font-size:16px;font-family:Verdana">&yen; {$result['price']}</font><br />
                有效期：<font style="font-size:16px;font-family:Verdana">{$expire}</font><br />
            </blockquote>
            <hr size=1 />
            <h3>附加信息如下:</h3>
            <blockquote>{$memo}</blockquote>
            <hr size=1 />
            <font style="font-size:12px" size="2">
            如果您在购买的时候选择了 Email 发送，你的朋友会在指定的日期收到乐铺的礼品卡通知邮件，该邮件会同时抄送给您。否则您需要将上面的代码自行通知给你的朋友。<br />
            您的朋友使用了此礼品卡后，我们会再通过 Email 方式通知您。<br />
            有任何问题可以参考 http://www.loopo.cn/gift 上的说明，也欢迎您随时联系我们。
            </font>
            </div>
            <hr size=1 />
            <div align="right" style="font-size:11px;color:#999">LooPo.cn - 最具创意的礼品店<br />{$date}</div>
BODY;

        $this->setBody($body);
        $this->send($f);

        if (!$meta['esend']) return;

        $this->reset();
        $this->setSubject($data['linkman'].":你收到了一张乐铺礼品卡");
        $this->setSendTo($data['linkman']);
        $this->setMailTo($meta['remail']);
        $this->isHTML(true);
        $this->setSendTime($meta['esendtime']);

        $mail['sendto'] = $data['linkman'];
        $mail['code']   = $result['str'];
        $mail['price']  = $result['price'];
        $mail['cman']   = $data['cman'];
        $mail['message']= $meta['message'];

        ob_start();
        require $tpl;
        $this->setBody(ob_get_contents());
        ob_end_clean();

        $this->send($f);

        //给购买人
        $this->setSendTo($data['cman']);
        $this->setMailTo($data['cemail']);
        $this->subject = 'Fwd:'.$this->subject;
        $this->body = "以下是乐铺发送给您的朋友 {$meta['remail']} 的信息 <hr size=1 /><blockquote>{$this->body}</blockquote>";

        return $this->send($f);
    }

    function send($data) {
        $data['subject']    = $this->subject;
        $data['mailto']     = $this->mailto;
        $data['sendto']     = $this->sendto;
        $data['ishtml']     = $this->ishtml;
        $data['body']       = $this->body;
        $data['sendtime']   = $this->sendtime;
        $data['created']    = time();

        if ($data['ishtml']) {
            $this->body = "<table width=600 border=0 align=center><tr><td> {$this->body} </td></tr></table>";
            //$this->body = "<center>如果您看不到邮件内容，请点击这里查看</center>".$this->body;
        }

        return $this->model->send($data);
    }

    function setSubject($str) {
        $this->subject = trim($str);
    }

    function setSendTo($sendto) {
        $this->sendto = $sendto;
    }

    function setMailTo($mailto) {
        $this->mailto = $mailto;
    }

    function isHTML($flag) {
        $this->ishtml = intval($flag);
    }

    function setBody($body) {
        $this->body .= $body;
    }

    function setSendTime($time) {
        if (!is_int($time))
            $this->sendtime = strtotime($time);
        else
            $this->sendtime = $time;
    }

    function reset() {
        $this->subject = '';
        $this->sendto = '';
        $this->mailto = '';
        $this->body = '';
        $this->ishtml = 0;
    }

    /**
     * 发送的 cron
     *
     * @access public
     * @return void
     */
    function sendMail() {
        $result = $this->model->sendMail();

        switch($result['code']['sendMail']) {
        case '1':
            $msg = 'a mail has been send.';
            break;
        case '2':
            $msg = 'mail send error.msg:'.$result['err'];
            $die = true;
            break;
        case '4':
            $msg = 'no mail have to send.';
            break;
        default:
            $msg = 'unknow stat.';
            $die = true;
        }

        if ($die) die($msg);
    }
}
?>
